import random


# 设计宠物的模板
class Pet:
    # 构造函数中定义属性
    def __init__(self, mz, lx, hp=None, mood=None):
        self.name = mz
        self.type = lx
        self.hp = hp if hp is not None else random.randint(80, 99)
        self.mood = mood if mood else random.randint(80, 99)

    # 输出宠物信息
    def info(self):
        print(f"我是一只{self.type}，名叫{self.name}，当前体力：{self.hp}，当前精力：{self.mood}")

    # 吃
    def eat(self, food):
        print(f"{self.name}正在吃{food}")
        self.hp += 10

    # 玩耍
    def play(self, toy):
        print(f"{self.name}正在玩{toy}")
        self.hp -= 10
        self.mood += 10


# 宠物机器类
# 负责在合适的时机创建一个宠物
# 输出宠物信息、吃、玩等功能
class Machine:
    # 每个宠物机都有一个属性：创建好的宠物对象
    def __init__(self):
        self.pet = None

    # 创建宠物
    def create_pet(self):
        type_list = ['小狗', '小猫', '鹦鹉', '蜥蜴', '蜘蛛']
        name_list = ['小汪', '小喵', '小五', '小西', '小珠']
        # 随机名字和类型
        type = random.choice(type_list)
        name = random.choice(name_list)
        # 将创建好的宠物对象保存
        self.pet = Pet(lx=type, mz=name)

    # 宠物打招呼
    def say_hello(self):
        if self.pet:  # 相当于 self.pet is not None
            self.pet.info()
        else:
            print("请先生成宠物")

    # 宠物喂养
    def feed(self):
        food_list = ['虫子', '磨牙棒', '冻干', '饲料']
        for idx, food in enumerate(food_list):
            print(idx + 1, food)
        idx = int(input("请选择要投喂的食物"))
        food = food_list[idx - 1]
        self.pet.eat(food)

    # 宠物玩耍
    def play_with_pet(self):
        toy_list = ['盘子', '冒险团', '小球', '玩具']
        for idx, toy in enumerate(toy_list):
            print(idx + 1, toy)
        idx = int(input("请选择要玩耍的玩具"))
        toy = toy_list[idx - 1]
        self.pet.play(toy)

    def menu(self):
        print("1.生成宠物")
        print("2.查看信息")
        print("3.投喂宠物")
        print("4.宠物玩耍")
        inp = int(input("请选择"))
        if inp == 1:
            self.create_pet()
        elif inp == 2:
            self.say_hello()
        elif inp == 3:
            self.feed()
        elif inp == 4:
            self.play_with_pet()
        self.menu()


if __name__ == '__main__':
    machine = Machine()
    machine.menu()
